package com.ruoyi.gp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gp.domain.vo.QuestionVo;
import com.ruoyi.gp.mapper.QuestionMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gp.domain.bo.SurveyBo;
import com.ruoyi.gp.domain.vo.SurveyVo;
import com.ruoyi.gp.domain.Survey;
import com.ruoyi.gp.mapper.SurveyMapper;
import com.ruoyi.gp.service.ISurveyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 问卷Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RequiredArgsConstructor
@Service
public class SurveyServiceImpl implements ISurveyService {

    private final SurveyMapper baseMapper;

    private final QuestionMapper questionMapper;

    /**
     * 查询问卷
     */
    @Override
    public SurveyVo queryById(Long surveyId){
        return baseMapper.selectVoById(surveyId);
    }

    /**
     * 查询问卷列表
     */
    @Override
    public TableDataInfo<SurveyVo> queryPageList(SurveyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Survey> lqw = buildQueryWrapper(bo);
        Page<SurveyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询问卷列表
     */
    @Override
    public List<SurveyVo> queryList(SurveyBo bo) {
        LambdaQueryWrapper<Survey> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Survey> buildQueryWrapper(SurveyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Survey> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), Survey::getTitle, bo.getTitle());
        lqw.eq(bo.getStartTime() != null, Survey::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, Survey::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Survey::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增问卷
     */
    @Override
    public Boolean insertByBo(SurveyBo bo) {
        Survey add = BeanUtil.toBean(bo, Survey.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSurveyId(add.getSurveyId());
        }
        return flag;
    }

    /**
     * 修改问卷
     */
    @Override
    public Boolean updateByBo(SurveyBo bo) {
        Survey update = BeanUtil.toBean(bo, Survey.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Survey entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除问卷
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
            ids.stream().forEach(id -> {
                QuestionVo questionVo = questionMapper.queryBySurvey(id);
                if(ObjectUtil.isNotEmpty(questionVo)) throw new ServiceException("存在二级分类，不能删除");
            });
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
